package com.sqx.modules.tbOrders.controller;

import com.sqx.common.annotation.SysLog;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.dao.SysEvaluateDao;
import com.sqx.modules.app.entity.SysEvaluate;
import com.sqx.modules.tbOrders.entity.SysComplaint;
import com.sqx.modules.tbOrders.entity.TbOrders;
import com.sqx.modules.tbOrders.entity.vo.TbOrdersVO;
import com.sqx.modules.tbOrders.service.ComplaintService;
import com.sqx.modules.tbOrders.service.TbOrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @description：TODO
 * @author： qiao
 * @create： 2024/7/10 13:30
 */
@RestController
@RequestMapping("/admin/orders")
@Api(value = "订单管理-管理端", tags = "{订单管理-管理端}")
@Slf4j
public class AdminTbOrdersController {

    @Autowired
    private TbOrdersService tbOrdersService;
    @Autowired
    private ComplaintService complaintService;
    @Autowired
    private SysEvaluateDao sysEvaluateDao;

    private final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(true);

    @GetMapping("/selectTbOrdersList")
    @ApiOperation("获取管理端普通商品订单列表")
    public Result selectTbOrdersList(@ApiParam("页码") Integer page,
                                     @ApiParam("条数") Integer limit,
                                     @ApiParam("开始时间") String startTime,
                                     @ApiParam("结束时间") String endTime,
                                     @ApiParam("订单实体") TbOrdersVO tbOrdersVO) {
        return Result.success().put("data", tbOrdersService.selectTbOrdersList(page, limit, startTime, endTime, tbOrdersVO));
    }

    @GetMapping("/selectTbOrdersListByType")
    @ApiOperation("获取管理端回收订单列表")
    public Result selectTbOrdersListByType(@ApiParam("页码") Integer page,
                                           @ApiParam("条数") Integer limit,
                                           @ApiParam("开始时间") String startTime,
                                           @ApiParam("结束时间") String endTime,
                                           @ApiParam("订单实体") TbOrdersVO tbOrdersVO) {
        return Result.success().put("data", tbOrdersService.selectTbOrdersListByType(page, limit, startTime, endTime, tbOrdersVO));
    }

    @PostMapping("/updateOrderByImg")
    @ApiOperation("管理端修改回收订单质检报告")
    public Result updateOrderByImg(@ApiParam("订单实体") TbOrders tbOrders) {
        return tbOrdersService.updateOrderByImg(tbOrders);
    }

    @DeleteMapping("/deleteTbOrders")
    @ApiOperation("删除管理端订单")
    public Result deleteTbOrders(@ApiParam("订单id") Long orderId) {
        return tbOrdersService.removeById(orderId) ? Result.success() : Result.error();
    }

    @PostMapping("/updateOrder")
    @ApiOperation("管理端修改或取消回收订单")
    public Result updateOrder(@ApiParam("订单实体") TbOrders tbOrders) {
        return tbOrdersService.updateOrder(tbOrders);
    }

    /**
     * 取消订单
     *
     * @param orderId 订单中心id
     */
    @ApiOperation("取消订单")
    @PostMapping(value = "/cancelOrder")
    public Result cancelOrder(@ApiParam("订单id") Long orderId) {
        return tbOrdersService.appCancelOrder(null, orderId);
    }

    @ApiOperation("查看全部投诉人的投诉")
    @GetMapping(value = "/findAllAppeal")
    public Result findAllAppeal(@ApiParam("页码") Integer page,
                                @ApiParam("条数") Integer limit,
                                @ApiParam("投诉实体") SysComplaint complaint) {
        return Result.success().put("data", complaintService.findAllAppeal(page, limit, complaint));
    }

    @ApiOperation("审核用户投诉")
    @SysLog(value = "审核用户投诉", type = "1")
    @GetMapping(value = "/updateCustomerComplaint")
    public Result updateCustomerComplaint(@ApiParam("投诉id") Long complaintId,
                                          @ApiParam("本投诉状态（1可申诉；2申诉中；3申诉未通过；4申诉通过；5用户发起投诉；6用户投诉被拒绝）") Integer complaintState) {
        return complaintService.updateCustomerComplaint(complaintId, complaintState);
    }

    @ApiOperation("是否通过被投诉人的申诉")
    @SysLog(value = "是否通过被投诉人的申诉", type = "1")
    @PostMapping(value = "/updateComplaintState")
    public Result updateComplaintState(@ApiParam("投诉实体") @RequestBody SysComplaint sysComplaint) {
        return complaintService.updateComplaintState(sysComplaint);
    }

    @ApiOperation("指派回收人员")
    @PostMapping(value = "/assignRecycleUser")
    @SysLog(value = "指派回收人员", type = "1")
    public Result assignRecycleUser(@ApiParam("订单id") Long orderId,
                                    @ApiParam("回收人员id") Long userId) {
        return tbOrdersService.assignRecycleUser(orderId, userId);
    }

    @ApiOperation("查看评价列表")
    @GetMapping(value = "/getEvaluateList")
    public Result getEvaluateList(@ApiParam("页码") Integer page,
                                  @ApiParam("条数") Integer limit,
                                  @ApiParam("评价实体") SysEvaluate sysEvaluate) {
        return tbOrdersService.getEvaluateList(page, limit, sysEvaluate);
    }

    @ApiOperation("审核评价订单")
    @PostMapping(value = "/updateEvaluateState")
    public Result updateEvaluateState(@ApiParam("评价实体") SysEvaluate sysEvaluate) {
        return tbOrdersService.updateEvaluateState(sysEvaluate);
    }

    @GetMapping("/selectOrdersCountStatisticsByYear")
    @ApiOperation("订单数量统计")
    public Result selectOrdersCountStatisticsByYear(String startTime, String endTime) {
        List<Integer> allOrdersCounts = new ArrayList<>();
        List<Integer> ordinaryOrders = new ArrayList<>();
        List<Integer> retrieveCountList = new ArrayList<>();
//        List<Integer> ordersDaiFuKuanCountList=new ArrayList<>();
//        List<Integer> ordersYiZhiFuCountList=new ArrayList<>();
//        List<Integer> ordersYiTuiKuanLunCountList=new ArrayList<>();
        List<String> year = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(parse);
        while (true) {
            String dateTime = simpleDateFormat.format(calendar.getTime());
            // 总订单数
            Integer allOrdersCount = tbOrdersService.selectOrdersCountStatisticsByYear(1, dateTime, 3);
            allOrdersCounts.add(allOrdersCount);
            //普通总订单数
            Integer ordinaryOrdersCount = tbOrdersService.selectOrdersCountStatisticsByYear(1, dateTime, 1);
            ordinaryOrders.add(ordinaryOrdersCount);
            // 回收订单总数
            Integer retrieveCount = tbOrdersService.selectOrdersCountStatisticsByYear(1, dateTime, 2);
            retrieveCountList.add(retrieveCount);
//            //0待支付
//            Integer ordersDaiFuKuanCount = tbOrdersService.selectOrdersCountStatisticsByYear(1, dateTime, 0);
//            ordersDaiFuKuanCountList.add(ordersDaiFuKuanCount);
//            //1已支付
//            Integer ordersJinXinCount = tbOrdersService.selectOrdersCountStatisticsByYear(1, dateTime, 1);
//            ordersYiZhiFuCountList.add(ordersJinXinCount);
//            //2已退款
//            Integer ordersQuXiaoCount = tbOrdersService.selectOrdersCountStatisticsByYear(1, dateTime, 2);
//            ordersYiTuiKuanLunCountList.add(ordersQuXiaoCount);
            year.add(dateTime);
            if (dateTime.equals(endTime)) {
                break;
            }
            calendar.add(Calendar.DATE, 1);
        }
        Map<String, Object> result = new HashMap<>();
        //普通订单总数
        result.put("ordersCountList", ordinaryOrders);
        // 回收订单总数
        result.put("retrieveCountList", retrieveCountList);
        // 总订单数
        result.put("allOrdersCounts", allOrdersCounts);
//        result.put("ordersDaiFuKuanCountList",ordersDaiFuKuanCountList);
//        result.put("ordersYiZhiFuCountList",ordersYiZhiFuCountList);
//        result.put("ordersYiTuiKuanLunCountList",ordersYiTuiKuanLunCountList);
        result.put("year", year);
        return Result.success().put("data", result);
    }

    @ApiOperation("发货")
    @PostMapping(value = "/sendGoods")
    public Result sendGoods(@ApiParam("订单实体") TbOrders tbOrders) {
        return tbOrdersService.sendGoods(tbOrders);
    }

    @ApiOperation("根据快递单号查询物流信息")
    @GetMapping(value = "/getLogistics")
    public Result getLogistics(@ApiParam("快递单号") String trackingNumber) {
        return tbOrdersService.getLogistics(trackingNumber);
    }

    @PostMapping("/updateRecycleOrder")
    @ApiOperation("更新回收订单")
    public Result updateRecycleOrder(@ApiParam("订单实体") TbOrders tbOrders) {
        return tbOrdersService.updateRecycleOrderByAdmin(tbOrders);
    }

    @PostMapping("/updateOrderByAdmin")
    @ApiOperation("管理员修改回收订单")
    public Result updateOrderByAdmin(@ApiParam("订单实体") TbOrders tbOrders) {
        return tbOrdersService.updateOrderByAdmin(tbOrders);
    }

    @PostMapping("/deleteComment")
    @ApiOperation("删除评论")
    public Result deleteComment(@ApiParam("评论id") Long evaluateId) {
        return Result.upStatus(sysEvaluateDao.deleteById(evaluateId));
    }

}
